package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.geom.RectangularShape;
import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 新增
     * @param dto
     * @return
     */
    @Override
    public Result addCategory(CategoryDTO dto) {
        Category category = new Category();
        //拷贝实体，传递参数
        BeanUtils.copyProperties(dto,category);
        //补全参数
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //从当前线程上获取当前用户id
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setStatus(0);
        categoryMapper.insert(category);
        return Result.success();
    }

    @Override
    public Result queryByPage(Integer page, Integer pageSize, String name, Integer type) {
        //开启分页，设置参数
        PageHelper.startPage(page,pageSize);
        Page<Category> categoryPage = categoryMapper.queryByPage(page,pageSize,name,type);

        //封装数据
        PageResult pageResult = new PageResult();
        pageResult.setTotal(categoryPage.getTotal());
        pageResult.setRecords(categoryPage.getResult());

        return Result.success(pageResult);
    }
}
